﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Classes;

namespace OfficeManager_v0._1
{
    public partial class AddOrEditDepartment : Form
    {
        Organization mOrganization;
        bool edit;
        Department oldDepartment;

        public AddOrEditDepartment(Organization aOrganization, bool edit, Department aDepartment = null)
        {
            mOrganization = aOrganization;
            this.edit = edit;

            InitializeComponent();

            comboBoxManager.DropDownStyle = ComboBoxStyle.DropDownList;

            comboBoxManager.Enabled = false;
            foreach (Worker worker in mOrganization.Workers)
            {
                if ( ((!edit && worker.Department != null) || (edit && worker == aDepartment.Manager)
                    || (edit && worker.Department != aDepartment)) && !(edit && worker.Department == null))
                    continue;
                comboBoxManager.Enabled = true;
                comboBoxManager.Items.Add(worker.Name);
            }

            if (edit)
            {
                this.oldDepartment = aDepartment;
                textBoxBudget.Text = oldDepartment.Budget.ToString();
                textBoxName.Text = oldDepartment.Name;
            }
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            try
            {
                Worker manager = mOrganization.Workers.Find(
                    (man) =>
                    {
                        return string.Compare(comboBoxManager.GetItemText(comboBoxManager.SelectedItem), man.Name) == 0;
                    });
                if (manager == null && comboBoxManager.GetItemText(comboBoxManager.SelectedItem) != "")
                    throw new Exception();
                if (manager != null) manager.CurrentJob = new Worker.JobEntry("Manager", manager.CurrentJob.Payment,
                        manager.CurrentJob.HireDate);

                if (!edit)
                {
                    var department = new Department();
                    department.Budget = int.Parse(textBoxBudget.Text);
                    department.Offices = new List<Office>();
                    department.Projects = new List<Project>();
                    department.Workers = new List<Worker>();
                    department.Manager = manager;
                    department.Name = textBoxName.Text;

                    if (manager != null)
                    {
                        manager.Department = department;
                        if (!oldDepartment.Workers.Contains(manager))
                            department.Workers.Add(manager);
                    }

                    mOrganization.Departments.Add(department);
                }
                else
                {
                    oldDepartment.Budget = int.Parse(textBoxBudget.Text);
                    if (oldDepartment.Manager != null) oldDepartment.Manager.UnassignWorker();
                    oldDepartment.Manager = manager;
                    oldDepartment.Name = textBoxName.Text;
                    if (manager != null)
                    {
                        manager.Department = oldDepartment;
                        if (!oldDepartment.Workers.Contains(manager))
                            oldDepartment.Workers.Add(manager);
                    }
                }
            }
            catch (Exception exc)
            {
                MessageBox.Show("Въведените данни са некоректни!");
                return;
            }

            this.Close();
            this.Dispose();
        }
    }
}
